<?php

namespace Modules\Ui\component;
use Duxravel\Core\UI\Tools;
use Duxravel\Core\UI\Widget\Icon;

use Duxravel\Core\UI\Table\Node;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Js;
use Modules\System\Admin\Common;
use Modules\Ui\component\Layout\Row;
use Modules\Ui\Traits\Makenable;

/**
 * 通用部件
 * @method static Row row(callable $callback = NULL)
 */
abstract class Content extends Common implements Renderable
{
    use Makenable;

    private string $style = "";
    private array $script = [];
    private array $scriptReturn = [];
    private string $tpl = "";


    /**
     * Node constructor.
     * @param string      $url
     * @param string      $key
     * @param string|null $title
     */
    public function __construct($tpl = '')
    {

    }


    /**
     * 设置style样式
     * @return $this
     */
    public function style($style = ''): Content
    {
        $this->style = $style;
        return $this;
    }


    /**
     * 添加变量
     * @param string $name 变量名
     * @param array|string $value 变量值
     * @param string $var 变量定义关键字 [var let const]
     * @return $this
     */
    public function addVariable(string $name = '', $value = '', string $var = 'let'): Content
    {

        if (is_array($value)){
            $value = Js::from($value);
        }
        $content = "{$var} {$name} = {$value}";
        $this->script[] = $content;
        return $this->addReturn($name);
    }


    /**
     * 添加变量与返回变量
     * @param mixed $content 定义变量内容 如 const a = 123
     * @param string $return 定义return返回变量
     * @return $this
     */
    public function addScript($content, string $return = ''): Content
    {
        if ($content instanceof \Closure) {
            $this->script[] = $content();
        } else {
            $this->script[] = $content;
        }
        return $this->addReturn($return);
    }

    /**
     * 添加返回变量
     * @param string $return 定义return返回变量
     * @return $this
     */
    public function addReturn(string $return = ''): Content
    {
        if ($return){
            $this->scriptReturn[] = $return;
        }
        return $this;
    }

    /**
     * 获取样式
     * @return string
     */
    public function getStyle(): string
    {
        return <<<HTML
<style>
    {$this->style}
</style>
HTML;
    }

    /**
     * @return string
     */
    public function getScript(): string
    {
        $setup = implode("\n", $this->script) . "\n" . ' return {' . implode(",", $this->scriptReturn) . '}';
        return <<<JS
const { ref, unref, computed, watch } = window.Vue;
{$setup}
JS;

    }

//    /**
//     * 模板赋值
//     * @param $name
//     * @param $value
//     */
//    public function assign($name, $value): void
//    {
//        $this->assign[$name] = $value;
//    }


    public function render()
    {
//        $style = $this->getStyle();
        $script = $this->getScript();
//        admin_inject_section("SCRIPT_SESSION",$script);
//        admin_inject_section("STYLE_SESSION",$style);
//        return $this->systemView($this->tpl);
        return app_success('ok', [
            'node' => [
                'nodeName' => 'app-layout',
                'child' => '测试'
            ],
            'setupScript'=>$script
        ]);
    }

}

